package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.CarBrandModel;
import com.czy.owner.utils.MyLog;
import com.czy.owner.widget.RoundImageView;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by King on 2016/6/13 0013.
 */
public class PickBrandAdapter extends BaseRecyclerAdapter<CarBrandModel, PickBrandAdapter.PickBrandHolder> implements RecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    private View mHeaderView;
    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_NORMAL = 1;  //说明是不带有header
    public PickBrandAdapter(Context context, List<CarBrandModel> data) {
        super(context, data);
    }

    @Override
    public PickBrandHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_pick_brand_recycler, parent, false);
        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new PickBrandHolder(mHeaderView);
        }
        return new PickBrandHolder(view);
    }

    @Override
    public void onBindViewHolder(PickBrandHolder holder, int position) {


        if(getItemViewType(position) == TYPE_NORMAL ){

            if(mHeaderView == null) {
                super.onBindViewHolder(holder, position);
                holder.bindData(data.get(position));
            }else {
                super.onBindViewHolder(holder, position -1 );
                holder.bindData(data.get(position - 1));
                return;
            }
        }else if(getItemViewType(position) == TYPE_HEADER){
            return;
        }
    }


    @Override
    public long getHeaderId(int position) {
        if(getItemViewType(position) == TYPE_HEADER){
            return "热门品牌".charAt(0);
        }
        return data.get(position-1).getFirstLetter().charAt(0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.recycler_contacts_header, parent, false);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView textView = (TextView) holder.itemView.findViewById(R.id.tv_recycler_header);

        if(getItemViewType(position) == TYPE_HEADER){
            textView.setText("热门品牌");
        }else {
            String showValue = String.valueOf(data.get(position).getFirstLetter().charAt(0));
            textView.setText(showValue);
        }
    }

    public int getPositionForSection(char section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = data.get(i).getFirstLetter();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }



    public String[] getArraySortLetters() {
        if (data.size() == 0) {
            return null;
        } else {
            LinkedList<String> listSortLetters = new LinkedList<>();
            for (int i = 0; i < data.size(); i++) {
                if (i == 0 || !listSortLetters.getLast().equals(data.get(i).getFirstLetter().toUpperCase())) {
                    listSortLetters.add(data.get(i).getFirstLetter().toUpperCase());
                }
            }
            String[] arraySortLetters = new String[listSortLetters.size()];
            arraySortLetters = listSortLetters.toArray(arraySortLetters);
            return arraySortLetters;
        }
    }

//    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView
    @Override
    public int getItemCount() {
        if(mHeaderView != null ){
            return data.size() + 1;
        } else{
            return data.size();
        }
    }

    public class PickBrandHolder extends BaseRecyclerAdapter.ViewHolder {
        private RoundImageView ivAvatar;
        private TextView tvTitle;

        public PickBrandHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView){
                return;
            }
            ivAvatar = (RoundImageView) itemView.findViewById(R.id.iv_pick_brand_item_avatar);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_pick_brand_item_title);
        }

        public void bindData(CarBrandModel mMembers) {
            if (mMembers.getImageSrc() != null && !mMembers.getImageSrc().isEmpty()) {
                ivAvatar.setTag(mMembers.getImageSrc());
                x.image().bind(ivAvatar, mMembers.getImageSrc(), new Callback.CommonCallback<Drawable>() {
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        ivAvatar.setImageResource(R.mipmap.default_icon);
                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                        ivAvatar.setImageResource(R.mipmap.default_icon);
                    }

                    @Override
                    public void onSuccess(Drawable result) {
                    }
                });
            } else {
                ivAvatar.setImageResource(R.mipmap.default_icon);
            }
            tvTitle.setText(mMembers.getChName());
        }


    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null ){
            return TYPE_NORMAL;
        }
        if (position == 0){
            //第一个item应该加载Header
            return TYPE_HEADER;
        }
        return TYPE_NORMAL;
    }

}
